<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="租户名称" prop="Q_clientName_S_LK">
            <el-input v-model="queryData.Q_clientName_S_LK" style="width:400px" />
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <el-col :span="1.5">
            <el-button v-permission="['system:oauth:add']" type="primary" size="mini" plain icon="el-icon-plus" @click="handleInfo()">
              新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-permission="['system:oauth:delete']" v-if="selectionData.length > 0" type="danger" size="mini" plain icon="el-icon-delete" @click="handleDelete()">
              删除
            </el-button>
          </el-col>
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" :columns="columns" />
        </el-row>
        <el-table
          ref="queryTable"
          v-loading="tableLoading"
          row-key="clientId"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column v-if="columns[0].visible" align="center" label="租户名称" prop="clientName" width="120" sortable show-overflow-tooltip>
            <template slot-scope="scope">
              <span style="cursor: pointer" @click="handleInfo(scope.row.clientId)">{{ scope.row.clientName }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="columns[1].visible" align="center" label="资源限定串" prop="resourceIds">
            <template v-if="scope.row.resourceIds" slot-scope="scope">
               <span v-for="(i,index) in scope.row.resourceIds.split(',')" :key="'tag_' + index">
                <el-tag v-if="index === 0" class="m-r-5">{{ scope.row.resourceIds.split(',')[0] }}</el-tag>
                <el-tag v-if="index === 1" class="m-r-5">{{ scope.row.resourceIds.split(',')[1] }}</el-tag>
              </span>
              <el-tooltip v-if="scope.row.resourceIds.split(',').length > 2" placement="top">
                <div slot="content">
                    <span v-for="(i,index) in scope.row.resourceIds.split(',')" :key="'content_' + index">
                      {{ i }}<br />
                    </span>
                </div>
                <el-tag v-if="scope.row.resourceIds.split(',').length > 2">{{ scope.row.resourceIds.split(',')[2] + ' ...' }}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column v-if="columns[2].visible" align="center" label="授权方式" prop="authorizedGrantTypes" width="320">
            <template v-if="scope.row.authorizedGrantTypes" slot-scope="scope">
              <span v-for="(i,index) in scope.row.authorizedGrantTypes.split(',')" :key="'tag_' + index">
                <el-tag v-if="index === 0" class="m-r-5">{{ scope.row.authorizedGrantTypes.split(',')[0] }}</el-tag>
                <el-tag v-if="index === 1" class="m-r-5">{{ scope.row.authorizedGrantTypes.split(',')[1] }}</el-tag>
              </span>
              <el-tooltip v-if="scope.row.authorizedGrantTypes.split(',').length > 2" placement="top">
                <div slot="content">
                    <span v-for="(i,index) in scope.row.authorizedGrantTypes.split(',')" :key="'content_' + index">
                      {{ i }}<br />
                    </span>
                </div>
                <el-tag v-if="scope.row.authorizedGrantTypes.split(',').length > 2">{{ scope.row.authorizedGrantTypes.split(',')[2] + ' ...' }}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column v-if="columns[3].visible" align="center" label="密钥" prop="clientSecretStr" width="150" />
          <el-table-column v-if="columns[4].visible" align="center" label="范围" prop="scope" width="100">
            <template v-if="scope.row.scope" slot-scope="scope">
              <el-tag
                v-for="name in scope.row.scope.split(',')"
                :key="'scope_' + name"
                type="success"
                class="m-r-5 m-b-5"
              >
                {{ name }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column v-if="columns[5].visible" label="访问令牌有效期(秒)" align="center" prop="accessTokenValidity">
            <template slot-scope="scope">
              {{ scope.row.accessTokenValidity }}
            </template>
          </el-table-column>
          <el-table-column v-if="columns[6].visible" label="刷新令牌有效期(秒)" align="center" prop="refreshTokenValidity">
            <template slot-scope="scope">
              {{ scope.row.refreshTokenValidity }}
            </template>
          </el-table-column>
          <el-table-column v-if="columns[7].visible" label="创建时间" align="center" prop="createTime" width="130">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100" fixed="right">
            <template slot-scope="scope">
              <el-dropdown>
                <span class="el-dropdown-link">
                  选项<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <el-link icon="el-icon-view" @click="handleInfo(scope.row.clientId)">查看</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['system:oauth:delete']">
                    <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.clientId)">删除</el-link>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>

    <oauth-form-dialog v-if="openFormDialog" ref="oauthForm" @confirmForm="handleFilter" />
  </div>
</template>

<script>
import { oauthTable, oauthDelete } from '@/api/system/oauth'
import OauthFormDialog from './form/dialog'

export default {
  components: {
    OauthFormDialog
  },
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_clientName_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: 'clientName',
        order: 'asc'
      },
      columns: [
        { key: 0, label: `租户名称`, visible: true },
        { key: 1, label: `资源限定串`, visible: true },
        { key: 2, label: `授权方式`, visible: true },
        { key: 3, label: `密钥`, visible: false },
        { key: 4, label: `范围`, visible: true },
        { key: 5, label: `访问令牌有效期`, visible: true },
        { key: 6, label: `刷新令牌有效期`, visible: true },
        { key: 7, label: `创建时间`, visible: true }
      ],
      selectionData: [],
      openFormDialog: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getTableData()
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      oauthTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.queryTable.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleInfo(id) {
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.oauthForm.init(id)
      })
    },
    handleDelete(id) {
      const ids = this.selectionData.map(function(row, index) {
        return row.clientId
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否删除所选?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        oauthDelete({ ids: id }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    }

  }
}
</script>
